/*
 * This file is part of "Sorting Thoughts" project.
 *
 * Copyright (c) 2009 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/sorting-thoughts/ for more information.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */
function StageAssistant(){
}

StageAssistant.prototype.setup = function(){
	
	Mojo.Log.info("#######################################");
    Mojo.Log.info("+++++++++++++++++++++++++++++++++++++++");
    Mojo.Log.info("A P P   S T A R T E D");
    Mojo.Log.info("+++++++++++++++++++++++++++++++++++++++");
	Mojo.Log.info("#######################################");
	
	if (this.mustShowStartPage()) {
		// first start or version update, go to start page
		Mojo.Log.info("Must do some DB updates!");
		this.controller.pushScene("start");
	} else {
		// update app settings
		this.updateSettings();
		
		if (SortingThoughts.enableAppLock && SortingThoughts.curLock.length > 0 && SortingThoughts.curLockTwo.length > 0) {
			this.controller.pushScene({
	        	transition: Mojo.Transition.zoom,
	        	name: "lock"
	    	});
		} else {
			// database exist, go direct to overview page
			this.controller.pushScene({
		        transition: Mojo.Transition.zoom,
		        name: "overview"
		    }, SortingThoughts.dao.thoughts, true);
		}
	}
};

StageAssistant.prototype.updateSettings = function() {
	Mojo.Log.info("Update settings");
	var now = new Date();
    var localOffset = now.getTimezoneOffset() * 60000;
	var sortingOrder = SortingThoughts.dao.getCurrentSortingOrderName();
	
	SortingThoughts.dao.updateSettings(localOffset, sortingOrder);
};

StageAssistant.prototype.mustShowStartPage = function() {
	var result = false;	
	
	// check first start
	if (SortingThoughts.firstStart) {
		Mojo.Log.info("First start detected.");
		result = true;
	}

	// check different versions
	if (SortingThoughts.oldVersionString != SortingThoughts.versionString) {
		Mojo.Log.info("Version change detected.");
		Mojo.Log.info("Old Version: " + SortingThoughts.oldVersionString);
		Mojo.Log.info("New Version: " + SortingThoughts.versionString);
		result = true;
	}
	
	return result;
};
